package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.UacThreshold;
import com.csun.cmny.provider.model.dto.threshold.UacThresholdSaveDto;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.DmcRpcService;
import com.csun.cmny.provider.service.UacThresholdService;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.google.common.collect.Lists;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-02-26 16:24
 * @Description: the class uac threshold controller
 */
@RestController
@RequestMapping(value = "/threshold", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class UacThresholdController extends BaseController {

    @Resource
    private UacThresholdService uacThresholdService;

    @Resource
    private DmcRpcService dmcRpcService;
    @Resource
    private CmnyMessageSource cmnyMessageSource;

    /**
     * 查询阈值
     *
     * @return
     */
    //@LogAnnotation(isSaveResponseData = true, isSaveRequestData = true)
    @RequestMapping(value = "/show", method = RequestMethod.POST)
    public Wrapper showByGroupId() {

        UacThreshold uacThreshold = uacThresholdService.showByGroupId(getLoginAuthDto().getGroupId());

        //return WrapMapper.ok(uacThreshold);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), uacThreshold);
    }

    /**
     * 保存阈值
     *
     */
    @ValidateAnnotation
   // @LogAnnotation(isSaveResponseData = true, isSaveRequestData = true)
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Wrapper save(@Validated @RequestBody UacThresholdSaveDto uacThresholdSaveDto, BindingResult br) {

        Integer result = uacThresholdService.save(uacThresholdSaveDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "操作失败");
        }

        // 推送保存阈值通知
        List<String> tagList = Lists.newArrayList("groupId_" + getLoginAuthDto().getGroupId());
        dmcRpcService.sendThresholdSaveMsg(tagList);

        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null), result);
    }

}
